package de.jeeyeon;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import de.jeeyeon.util.EmoKeyboard;

public class Modify_Entry_Activity extends Activity {

	private Button button2; //send entry button
	private EditText textarea;
	
	private Button emobutton;
	
	private String entry;
	private String id;
	
	private String newtext;
	
	private EmoKeyboard emoKeyboard;
	
	
	public static final String URL = "http://snuk87.sn.funpic.de/modify_entry.php";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify__entry_);
		
		textarea = (EditText) findViewById(R.id.textfield_entry);
		
		
		Bundle extras = getIntent().getExtras();
		entry = extras.getString("text");
		id = extras.getString("id");
		
		textarea.setText(entry);
				
		button2 = (Button) findViewById(R.id.send_button);
		setSendButtonListener();
		
		emobutton = (Button) findViewById(R.id.emoBtn);
		setEmoButtonListener();
		
		emoKeyboard = new EmoKeyboard(this, R.id.keyboardview, R.xml.emo, textarea);
		setKeyboardListener();
		
	}
	
	private void setKeyboardListener()
	{
		textarea.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v)
			{
				emoKeyboard.hideCustomKeyboard();
				
			}
		});
	}
	
	@Override public void onBackPressed() 
	{
	    if( emoKeyboard.isCustomKeyboardVisible() ) emoKeyboard.hideCustomKeyboard(); else this.finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.modify__entry_, menu);
		return true;
	}
	
	private void setEmoButtonListener()
	{
		emobutton.setOnClickListener(new OnClickListener() 
		{	
			@Override
			public void onClick(View v) 
			{
				emoKeyboard.showCustomKeyboard(v);	
			}
		});
	}
	
	private void setSendButtonListener()
	{
		button2.setOnClickListener(new OnClickListener() 
		{	
			@Override
			public void onClick(View arg0) 
			{
				new AsyncTask<String, Void, String>() 
				{
					@Override
					protected String doInBackground(String... params) 
					{
						String output = null;
						try
						{
							newtext = textarea.getText().toString();
							
							DefaultHttpClient client = new DefaultHttpClient();
							HttpPost post = new HttpPost(URL);
							
					        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
					        nameValuePairs.add(new BasicNameValuePair("id", id));
					        nameValuePairs.add(new BasicNameValuePair("entry", newtext));
					        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
					        
					        HttpResponse response = client.execute(post);
					        HttpEntity entity = response.getEntity();
					        output = EntityUtils.toString(entity);
					     }
						catch(Exception e){ e.printStackTrace(); Log.d("Catch: " + e.toString(), "jeejee"); }
						
						return output;
					}
					
					protected void onPostExecute(String result)
					{
						Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
						
						Intent returnIntent = new Intent();
						setResult(RESULT_OK, returnIntent);
						finish();
					}
				}.execute();
			}
		});
	}
	


}
